import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'
import { Response } from '../../components/type/general'
export const commonStore = defineStore('commonStore', {
	state: () => ({
		clientId: '',
		accountId: '',
		accIdList: [],
		shortcuts: [
			{
			  text: 'Today',
			  value: new Date(),
			},
			{
			  text: 'Yesterday',
			  value: () => {
				const date = new Date()
				date.setTime(date.getTime() - 3600 * 1000 * 24)
				return date
			  },
			},
			{
			  text: 'A week ago',
			  value: () => {
				const date = new Date()
				date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
				return date
			  },
			},
		],
	}),
	actions: {
		setClientId(val: any) {
			this.clientId = val
		},
		setAccountId(val: any) {
			this.accountId = val
		},
		setIdList(val: any) {
			this.accIdList = val
		},
	}
})
export function fetchWarning (res: Response) {
    // if (res.ret !== 200) {
    //   ElMessage.warning(res.msg)
    //   return true
    // }
    if (res.code !== 0) {
      ElMessage.warning(res.msg)
      return true
    }
    return false
  }
  export function myError (e: any) {
    if (e.ret === 403 && e.data?.code === 10001091) {
      setTimeout(() => {
        window.location.href = '/login'
      }, 1500)
    }
    if (e instanceof Error) {
      ElMessage.error(e.message)
      return
    }
    const errorMsg = e.msg ?? e.message ?? e.data?.message ?? e
    ElMessage.error(errorMsg)
  }